package com.sjgtw.cloud.system.controller;

import com.sjgtw.cloud.common.core.constant.UserConstants;
import com.sjgtw.cloud.common.core.exception.BaseException;
import com.sjgtw.cloud.common.core.utils.SecurityUtils;
import com.sjgtw.cloud.common.core.utils.poi.ExcelUtil;
import com.sjgtw.cloud.common.core.web.controller.BaseController;
import com.sjgtw.cloud.common.core.web.domain.AjaxResult;
import com.sjgtw.cloud.common.core.web.page.TableDataInfo;
import com.sjgtw.cloud.common.log.annotation.Log;
import com.sjgtw.cloud.common.log.enums.BusinessType;
import com.sjgtw.cloud.common.security.annotation.PreAuthorize;
import com.sjgtw.cloud.system.domain.SysDictType;
import com.sjgtw.cloud.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>Title: 数据字典类型管理</p>
 * <p>Description: </p>
 * @author Admin
 * @date 2020-7-4
 */
@RestController
@RequestMapping("/dict/type")
public class SysDictTypeController extends BaseController
{
	@Autowired
	private ISysDictTypeService dictTypeService;
	
	/**
     * <p>Title: 字典类型列表</p>
     * <p>Description: </p>
     * @param dictType
     * @return
     */
	@PreAuthorize(hasPermi = "system:dict:list")
	@GetMapping("/list")
	public TableDataInfo list(SysDictType dictType) {
		startPage();
		List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
		return getDataTable(list);
	}
	
	/**
	 * <p>Title: 字典类型导出</p>
	 * <p>Description: </p>
	 * @param response
	 * @param dictType
	 * @throws IOException
	 */
	@Log(title = "字典类型", businessType = BusinessType.EXPORT)
	@PreAuthorize(hasPermi = "system:dict:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictType dictType) throws IOException
    {
//        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
//        ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
//        util.exportExcel(response, list, "字典类型");
        throw new BaseException("字典类型数据为敏感数据，不提供数据导出功能！");
    }

	/**
	 * <p>Title: 字典类型列表</p>
	 * <p>Description: </p>
	 * @param dictType
	 * @return
	 */
	@GetMapping("/listNoAuth")
	public AjaxResult listNoAuth(SysDictType dictType) {
		List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
		return AjaxResult.success(list);
	}

	/**
	 * <p>Title: 查询字典类型详细</p>
	 * <p>Description: </p>
	 * @param dictId
	 * @return
	 */
	@PreAuthorize(hasPermi = "system:dict:query")
	@GetMapping(value = "/{dictId}")
	public AjaxResult getInfo(@PathVariable Long dictId) {
		return AjaxResult.success(dictTypeService.selectDictTypeById(dictId));
	}

	/**
	 * <p>Title: 新增字典类型</p>
	 * <p>Description: </p>
	 * @param dict
	 * @return
	 */
	@PreAuthorize(hasPermi = "system:dict:add")
	@Log(title = "字典类型", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@Validated @RequestBody SysDictType dict) {
		if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
			return AjaxResult.error("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
		}
		dict.setCreateBy(SecurityUtils.getUsername());
		return toAjax(dictTypeService.insertDictType(dict));
	}

	/**
	 * <p>Title: 修改字典类型</p>
	 * <p>Description: </p>
	 * @param dict
	 * @return
	 */
	@PreAuthorize(hasPermi = "system:dict:edit")
	@Log(title = "字典类型", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@Validated @RequestBody SysDictType dict) {
		if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
			return AjaxResult.error("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
		}
		dict.setUpdateBy(SecurityUtils.getUsername());
		return toAjax(dictTypeService.updateDictType(dict));
	}

	/**
	 * <p>Title: 删除字典类型</p>
	 * <p>Description: </p>
	 * @param dictIds
	 * @return
	 */
	@PreAuthorize(hasPermi = "system:dict:remove")
	@Log(title = "字典类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dictIds}")
	public AjaxResult remove(@PathVariable Long[] dictIds) {
		return toAjax(dictTypeService.deleteDictTypeByIds(dictIds));
	}

	/**
	 * <p>Title: 清空字典缓存数据</p>
	 * <p>Description: </p>
	 * @return
	 */
	@PreAuthorize(hasPermi = "system:dict:remove")
	@Log(title = "字典类型", businessType = BusinessType.CLEAN)
	@DeleteMapping("/clearCache")
	public AjaxResult clearCache() {
		dictTypeService.clearCache();
		return AjaxResult.success();
	}

	/**
	 * <p>Title: 重置字典数据缓存</p>
	 * <p>Description: </p>
	 * @return
	 */
	@PreAuthorize(hasPermi = "system:dict:remove")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public AjaxResult refreshCache()
    {
        dictTypeService.resetDictCache();
        return AjaxResult.success();
    }

	/**
	 * <p>Title: 获取字典选择框列表</p>
	 * <p>Description: </p>
	 * @return
	 */
	@GetMapping("/optionselect")
	public AjaxResult optionselect() {
		List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
		return AjaxResult.success(dictTypes);
	}
}
